package com.h1.staffmanage.controller;


import com.h1.staffmanage.entity.*;
import com.h1.staffmanage.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mengfanyu
 * @since 2022-06-23
 */
@RestController
@CrossOrigin
@RequestMapping("/staffmanage/t-employee")
public class TEmployeeController {
    @Autowired
    private TEmployeeService employeeService;
    @Autowired
    private TPoliticsStatusService politicsStatusService;
    @Autowired
    private TJoblevelService joblevelService;
    @Autowired
    private TNationService nationService;
    @Autowired
    private TPositionService positionService;
    @Autowired
    private TDepartmentService departmentService;

    @ApiOperation(value = "获取所有员工(分页)")
    @GetMapping("/")
    public RespPageBean getEmployee(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "10") Integer size,
                                    TEmployee employee,
                                    LocalDate[] beginDateScope) {
        return employeeService.getEmployeeByPage(currentPage, size, employee, beginDateScope);
    }

    @ApiOperation(value = "获取所有政治面貌")
    @GetMapping("/politicsStatus")
    public List<TPoliticsStatus> getAllPoliticsStatus() {
        return politicsStatusService.list(null);
    }

    @ApiOperation(value = "获取所有职称")
    @GetMapping("/joblevels")
    public List<TJoblevel> getAllJoblevels() {
        return joblevelService.list(null);
    }

    @ApiOperation(value = "获取所有民族")
    @GetMapping("/nations")
    public List<TNation> getAllNations() {
        return nationService.list(null);
    }

    @ApiOperation(value = "获取所有职位")
    @GetMapping("/positions")
    public List<TPosition> getAllPositions() {
        return positionService.list(null);
    }

    @ApiOperation(value = "获取所有部门")
    @GetMapping("/deps")
    public List<TDepartment> getAllDepartments() {
        return departmentService.getAllDepartments();
    }

    @ApiOperation(value = "获取工号")
    @GetMapping("/maxWorkID")
    public RespBean maxWorkID() {
        return employeeService.maxWorkID();
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/")
    public RespBean addEmp(@RequestBody TEmployee employee) {
        return employeeService.addEmp(employee);
    }

    @ApiOperation(value = "更新员工")
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody TEmployee employee) {
        if (employeeService.updateById(employee)) {
            return RespBean.success("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @ApiOperation(value = "删除员工")
    @DeleteMapping("/{id}")
    public RespBean deleteEmp(@PathVariable Integer id) {
        if (employeeService.removeById(id)) {
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

//    @ApiOperation(value = "导出员工数据")
//    @GetMapping(value = "/export", produces = "application/octet-stream")
//    public void exportEmployee(HttpServletResponse response) {
//        List<TEmployee> list = employeeService.getEmployee(null);
//        ExportParams params = new ExportParams("员工表", "员工表", ExcelType.HSSF);
//        Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, list);
//        ServletOutputStream outputStream = null;
//        try {
//            //流的形式
//            response.setHeader("content-type", "application/octet-stream");
//            //防止中文乱码
//            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("员工表.xls", "UTF-8"));
//            outputStream = response.getOutputStream();
//            workbook.write(outputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (outputStream != null) {
//                try {
//                    outputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

//    @ApiOperation(value = "导入员工数据")
//    @PostMapping("/import")
//    public RespBean importEmployee(MultipartFile file) {
//        ImportParams params = new ImportParams();
//        //去掉标题行
//        params.setTitleRows(1);
//        List<Nation> nationList = nationService.list();
//        List<PoliticsStatus> politicsStatuses = politicsStatusService.list();
//        List<Position> positions = positionService.list();
//        List<Department> departments = departmentService.list();
//        List<Joblevel> joblevels = joblevelService.list();
//        try {
//            List<Employee> list = ExcelImportUtil.importExcel(file.getInputStream(), Employee.class, params);
//            list.forEach(employee -> {
//                //民族id
//                employee.setNationId(nationList.get(nationList.indexOf(new Nation(employee.getNation().getName()))).getId());
//                //政治面貌id
//                employee.setPoliticId(politicsStatuses.get(politicsStatuses.indexOf(new PoliticsStatus(employee.getPoliticsStatus().getName()))).getId());
//                //部门id
//                employee.setDepartmentId(departments.get(departments.indexOf(new Department(employee.getDepartment().getName()))).getId());
//                //职称id
//                employee.setJobLevelId(joblevels.get(joblevels.indexOf(new Joblevel(employee.getJoblevel().getName()))).getId());
//                //职位id
//                employee.setPosId(positions.get(positions.indexOf(new Position(employee.getPosition().getName()))).getId());
//            });
//            if (employeeService.saveBatch(list)) {
//                return RespBean.success("导入成功!");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return RespBean.error("导入失败!");
//    }
}

